package cn.cl.gulimall.product.service.impl;

import cn.cl.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.Query;

import cn.cl.gulimall.product.dao.BrandDao;
import cn.cl.gulimall.product.entity.BrandEntity;
import cn.cl.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = params.get("key").toString();
		QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(key)) {
			queryWrapper
					.eq("brand_id", key)
					.or()
					.like("name", key);
		}
		IPage<BrandEntity> page = this.page(
				new Query<BrandEntity>().getPage(params),
				queryWrapper
		);
		return new PageUtils(page);
	}


	@Autowired
	CategoryBrandRelationService categoryBrandRelationService;

	/**
	 * 品牌级联更新
	 */
	@Transactional
	@Override
	public void updateDatail(BrandEntity brand) {
		// 保证冗余字段的数据一致
		this.updateById(brand);
		if (!StringUtils.isEmpty(brand.getName())) {
			// 同步更新其他关联表中的数据
			categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
			// TODO 更新其他关联
		}
	}
}